﻿@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>设备出库信息</title>
    <link href="~/Scripts/jquery-easyui-1.5.4/themes/bootstrap/easyui.css" rel="stylesheet" />
    <script src="~/Scripts/jquery-easyui-1.5.4/jquery.min.js"></script>
    <script src="~/Scripts/jquery-easyui-1.5.4/jquery.easyui.min.js"></script>
    <script src="~/Scripts/jquery-easyui-1.5.4/locale/easyui-lang-zh_CN.js"></script>
    <link href="~/Content/themes/icon.css" rel="stylesheet" />
    <script src="~/Content/js/common.js"></script>
    <link href="~/Content/themes/table.css" rel="stylesheet" />
    <script type="text/javascript">
        $(function () {
            $.ajax({     //请求当前用户可以操作的按钮
                url: "/Button/GetUserAuthorizeButton?r=" + Math.random(),
                type: "post",
                data: { "KeyCode": "Dpr", "KeyName": "Dpr" },
                dataType: "json",
                timeout: 5000,
                success: function (data) {

                    if (data.success) {
                        var toolbar = getToolBar(data);      //common.js
                        if (data.search) {     //判断是否有浏览权限
                            $("#ui_DevProRelation_dg").datagrid({
                                url: "/DevProRelation/GetAllDevProRelation",
                                striped: true, rownumbers: true, pagination: true, pageSize: 20, singleSelect: true,
                                idField: 'DeviceId',
                                fit: true,
                                fitColumns: false,
                                //sortName: 'UpdateTime',
                                //sortOrder: 'desc',
                                pageList: [20, 40, 60, 80, 100],
                                columns: [[
                                                 //{ width: 100, title: '设备编号', field: 'DeviceId' },
                                                 { width: 100, title: '设备名称', field: 'DeviceName' },
                                                 { width: 100, title: '基站名称', field: 'LocaName' },
                                                  { width: 100, title: '重命名', field: 'ReName' },

                                                 { width: 100, title: '设备类别', field: 'DeviceTypeName' },
                                                 { width: 100, title: '厂家序列号', field: 'FactorySerial' },
                                                 { width: 100, title: '公司序列号', field: 'CompanySerial' },
                                                 { width: 100, title: '厂家规格型号', field: 'FModelSize' },
                                                 { width: 100, title: '公司规格型号', field: 'CModelSize' },
                                                 { width: 100, title: '采购负责人', field: 'Purchaser' },
                                                 {
                                                     width: 100, title: '采购时间', field: 'PurchaseTime', formatter: function (date) {
                                                         var pa = /.*\((.*)\)/;
                                                         var unixtime = date.match(pa)[1].substring(0, 10);
                                                         return getTime(unixtime);
                                                     }
                                                 },
                                                 {
                                                     width: 100, title: '质保期', field: 'WarrantyTime', formatter: function (date) {
                                                         var pa = /.*\((.*)\)/;
                                                         var unixtime = date.match(pa)[1].substring(0, 10);
                                                         return getTime(unixtime);
                                                     }
                                                 },
                                                 { width: 100, title: '设备厂家', field: 'DeviceFactory' },
                                                 { width: 100, title: '联系电话', field: 'DeviceTel' },
                                                 {
                                                     width: 100, title: '状态', field: 'State', formatter: function (value, row, index) {
                                                         if (value == 0) {
                                                             return '未出库';
                                                         }
                                                         else if (value == 1) {
                                                             return "已出库";
                                                         }
                                                         else {
                                                             return "报废";
                                                         }
                                                     }
                                                 },

                                                 { width: 100, title: '出库类型', field: 'CodeName' },
                                                 {
                                                     width: 100, title: '出库/报废时间', field: 'OutTime', formatter: function (date) {
                                                         if (date == null) {
                                                             return;
                                                         }
                                                         var pa = /.*\((.*)\)/;
                                                         var unixtime = date.match(pa)[1].substring(0, 10);
                                                         return getTime(unixtime);
                                                     }
                                                 },
                                                 { width: 100, title: '设备等级', field: 'DeviceLevel', hidden: true },
                                                  //{
                                                  //    width: 100, title: '报废时间', field: 'ScrapTime', formatter: function (date) {
                                                  //        if (date == null) {
                                                  //            return;
                                                  //        }
                                                  //        var pa = /.*\((.*)\)/;
                                                  //        var unixtime = date.match(pa)[1].substring(0, 10);
                                                  //        return getTime(unixtime);
                                                  //    }
                                                  //},
                                                { width: 100, title: '备注', field: 'Remark' },


                                ]],

                                toolbar: toolbar.length == 0 ? null : toolbar,
                            });
                        }
                        else {
                            $.show_alert("提示", "无权限，请联系管理员！");
                        }
                    } else {
                        $.show_alert("错误", data.result);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    if (textStatus == "timeout") {
                        $.show_alert("提示", "请求超时，请刷新当前页重试！");
                    }
                    else {
                        $.show_alert("错误", textStatus + "：" + errorThrown);
                    }
                }
            })
        });

        //添加设备信息
        function AddDpr() {
            $("<div/>").dialog({
                id: "ui_DevProRelation_add_dialog",
                href: "/DevProRelation/DevProRelationAdd",
                title: "添加设备关系信息",
                height: 570,
                width: 910,
                modal: true,
                buttons: [{
                    id: "ui_DevProRelation_add_btn",
                    text: '添 加',
                    handler: function () {
                        //alert($('#ParentDev').combobox('getValue'));

                        if ($("#selCodeType").val() == '0') {
                            alert('请选出库途径类型');
                            return false;
                        }
                        //alert($('#selDev').combobox('getValue'));
                        if ($('#selDev').combobox('getValue') == '') {
                            alert('请选出库设备');
                            return false;
                        }

                        $("#DevProRelationAddForm").form("submit", {
                            url: "/DevProRelation/DevProRelationInfoAdd",
                            onSubmit: function (param) {
                                param.DeviceId = $('#selDev').combobox('getValue');
                                param.CodeId = $("#selCodeType").val();
                                param.Code = $("#txtCode").val();
                                param.ReName = $("#txtReName").val();
                                param.LocaId = $('#selLoca').combobox('getValue');
                                param.WarrantyTime = $("#txtWarrantyTime").datetimebox("getValue");
                                param.Remark = $("#txtRemark").val();
                                param.State = $("#selState").val();
                                param.ChangeDateTime = $("#txtDateTime").datetimebox("getValue");
                                if ($(this).form('validate')) {
                                    $('#ui_DevProRelation_add_btn').linkbutton('disable');
                                    return true;
                                }
                                else {
                                    $('#ui_DevProRelation_add_btn').linkbutton('enable');   //恢复按钮
                                    return false;
                                }
                            },
                            success: function (data) {
                                var dataJson = eval('(' + data + ')');
                                if (dataJson.success) {
                                    $("#ui_DevProRelation_add_dialog").dialog('destroy');
                                    $.show_alert("提示", dataJson.msg);
                                    $("#ui_DevProRelation_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                                } else {
                                    $('#ui_DevProRelation_add_btn').linkbutton('enable');
                                    $.show_alert("提示", dataJson.msg);
                                }
                            }
                        });
                    }
                }, {
                    text: '取 消',
                    handler: function () {
                        $("#ui_DevProRelation_add_dialog").dialog('destroy');
                    }
                }],
                onLoad: function () {
                    $("#txtDeviceId").focus();
                    //设置设备下拉列表
                    $('#selDev').combobox({
                        prompt: '输入首关键字自动检索',
                        required: false,
                        url: '/DevProRelation/GetSelDevInfo',
                        editable: true,
                        hasDownArrow: true,
                        filter: function (q, row) {

                            var opts = $(this).combobox('options');

                            return row[opts.textField].indexOf(q) >= 0;
                        }
                    });
                    //设置基站下拉列表
                    $('#selLoca').combobox({
                        prompt: '输入首关键字自动检索',
                        required: false,
                        url: '/DevProRelation/GetSelLocaInfo',
                        editable: true,
                        hasDownArrow: true,
                        filter: function (q, row) {

                            var opts = $(this).combobox('options');

                            return row[opts.textField].indexOf(q) >= 0;
                        }
                    });

                    $('#txtDateTime').datetimebox('setValue', formatterDate(new Date()));
                },
                onClose: function () {
                    $("#ui_DevProRelation_add_dialog").dialog('destroy');  //销毁dialog对象
                }
            });
        }

        //修改设备关系
        function EditDpr() {
            var rows = $("#ui_DevProRelation_dg").datagrid("getSelected");
            if (rows == null) {
                $.show_alert("提示", "请先选择修改的设备关系");
                return;
            }
            if (rows.State == 2) {
                $.show_alert("提示", "报废的设备不能编辑");
                return;
            }
            //alert(rows.DeviceLevel);
            if (rows.DeviceLevel > 1) {
                $.show_alert("提示", "当前设备等级不是1级，不能修改！");
                return;
            }

            var DeviceId = rows.DeviceId;
            $("<div/>").dialog({
                id: "ui_DevProRelation_edit_dialog",
                href: "/DevProRelation/DevProRelationEdit?DeviceId=" + DeviceId,
                title: "修改设备关系信息",
                height: 570,
                width: 910,
                modal: true,
                buttons: [{
                    id: "ui_DevProRelation_edit_btn",
                    text: '修 改',
                    handler: function () {
                        if ($("#selCodeType").val() == '') {
                            alert('请选择出库途径类型!');
                            return false;
                        }
                        $("#DevProRelationEditForm").form("submit", {
                            url: "/DevProRelation/DevProRelationInfoEdit",
                            onSubmit: function (param) {
                                param.DPRelationId = $("#txtID").val();
                                param.DeviceId = $('#txtDeviceId').val();
                                param.CodeId = $("#selCodeType").val();
                                param.Code = $("#txtCode").val();
                                param.ReName = $("#txtReName").val();
                                param.LocaId = $('#selLoca').combobox('getValue');
                                param.WarrantyTime = $("#txtWarrantyTime").datetimebox("getValue");
                                param.Remark = $("#txtRemark").val();
                                param.State = $("#selState").val();
                                param.ChangeDateTime = $("#txtDateTime").datetimebox("getValue");
                                if ($(this).form('validate')) {
                                    $('#ui_DevProRelation_edit_btn').linkbutton('disable');
                                    return true;
                                }
                                else {
                                    $('#ui_DevProRelation_edit_btn').linkbutton('enable');   //恢复按钮
                                    return false;
                                }
                            },
                            success: function (data) {
                                var dataJson = eval('(' + data + ')');
                                if (dataJson.success) {
                                    $("#ui_DevProRelation_edit_dialog").dialog('destroy');
                                    $.show_alert("提示", dataJson.msg);
                                    $("#ui_DevProRelation_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                                } else {
                                    $('#ui_DevProRelation_edit_btn').linkbutton('enable');
                                    $.show_alert("提示", dataJson.msg);
                                }
                            }
                        });
                    }
                }, {
                    text: '取 消',
                    handler: function () {
                        $("#ui_DevProRelation_edit_dialog").dialog('destroy');
                    }
                }],
                onLoad: function () {
                    $("#txtDeviceId").focus();
                    //设置设备下拉列表
                    $('#selDev').combobox({
                        prompt: '输入首关键字自动检索',
                        required: false,
                        url: '/DevProRelation/GetSelDevInfo',
                        editable: true,
                        hasDownArrow: true,
                        filter: function (q, row) {

                            var opts = $(this).combobox('options');

                            return row[opts.textField].indexOf(q) >= 0;
                        }
                    });
                    //设置基站下拉列表
                    $('#selLoca').combobox({
                        prompt: '输入首关键字自动检索',
                        required: false,
                        url: '/DevProRelation/GetSelLocaInfo',
                        editable: true,
                        hasDownArrow: true,
                        filter: function (q, row) {

                            var opts = $(this).combobox('options');

                            return row[opts.textField].indexOf(q) >= 0;
                        }
                    });
                },
                onClose: function () {
                    $("#ui_DevProRelation_edit_dialog").dialog('destroy');  //销毁dialog对象
                }
            });
        }

        //报废
        function Scrap() {
            var rows = $("#ui_DevProRelation_dg").datagrid("getSelected");
            if (rows == null) {
                $.show_alert("提示", "请先选择报废的设备关系");
                return;
            }
            if (rows.State == 2) {
                $.show_alert("提示", "当前设备已经为报废状态！");
                return;
            }

            var DeviceId = rows.DeviceId;
            $.messager.confirm('提示', '确定报废吗？', function (r) {
                if (r) {
                    $.ajax({
                        url: "/DevProRelation/DelDevProRelation",
                        data: {
                            DeviceId: DeviceId
                        },
                        type: "POST",
                        dataType: "json",
                        success: function (data) {
                            if (data.success) {
                                $.show_alert("提示", data.msg);
                                $("#ui_DevProRelation_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                $.show_alert("提示", data.msg);
                            }
                        }
                    });
                }
            });
        }


        function ui_Dev_searchdata() {


            validateDateTime();

            $("#ui_DevProRelation_dg").datagrid('load', {
                selReName: $('#selReName').val(),
                selCompanySerial: $('#selCompanySerial').val(),
                selCModelSize: $('#selCModelSize').val(),
                selStartWarrantyTime: $('#selStartWarrantyTime').val(),
                selEndWarrantyTime: $('#selEndWarrantyTime').val(),
                selDeviceLevel: $('#selDeviceLevel').val(),
                selDevState: $('#selDevState').val(),
            });
            $("#ui_DevProRelation_add_dialog").datagrid('clearSelections').datagrid('clearChecked');
        }

        function ui_Dev_cleardata() {
            $('#selReName').val('');
            $('#selCompanySerial').val('');
            $('#selCModelSize').val('');
            $('#selDevState').val('1'),
            $('#selStartWarrantyTime').datebox('setValue', '');
            $('#selEndWarrantyTime').datebox('setValue', '');
            $("#ui_DevProRelation_add_dialog").datagrid('clearSelections').datagrid('clearChecked');
        }

        //时间转换
        function getTime(/** timestamp=0 **/) {
            var ts = arguments[0] || 0;
            var t, y, m, d, h, i, s;
            t = ts ? new Date(ts * 1000) : new Date();
            y = t.getFullYear();
            m = t.getMonth() + 1;
            d = t.getDate();
            h = t.getHours();
            i = t.getMinutes();
            s = t.getSeconds();
            // 可根据需要在这里定义时间格式
            return y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d);
        }

        function validateDateTime() {
            console.log("hello world");

            var v1 = $("#selStartWarrantyTime").datetimebox('getValue');
            var date1 = new Date(v1);
            console.log(date1);
            var v2 = $("#selEndWarrantyTime").datetimebox('getValue');
            var date2 = new Date(v2);
            console.log(date2);
            if (v1 == '' || v2 == '') {
                return true;
            }
            console.log(date1);
            console.log(date2);
            if (date1 < date2) {
                console.log(date1 + "<" + date2);
                //var diff = DateDiff(date1, date2);
                //if (diff > 30) {
                //    $.messager.alert('提示', '时间段不能超过30天！');
                //}
                return true;
            }
            console.log(date1 + "!<" + date2);

            $.messager.alert('提示', '开始时间要小于结束时间！');

            return false;
        }

        //显示当前日期
        formatterDate = function (date) {
            var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
            var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0"
            + (date.getMonth() + 1);
            var hor = date.getHours();
            var min = date.getMinutes();
            var sec = date.getSeconds();
            return date.getFullYear() + '-' + month + '-' + day + " " + hor + ":" + min + ":" + sec;
        };

    </script>


</head>
<body>
    <div id="ui_Dev_layout" class="easyui-layout" data-options="fit:true,border:false">
        <div data-options="region:'north',split:true,border:true,collapsed:false" title="搜索条件" style="height:74px;">
            <div id="ui_user_search">
                <table class="tableForm" style="width: 99%; height: 100%; background: #F5F5F5;">
                    <tr>
                        <th>
                            重命名：
                        </th>
                        <td>
                            <input name="selReName" id="selReName" class="easyui-validatebox textbox" style="width:110px;height:22px;" />
                        </td>

                        <th>
                            公司序列号：
                        </th>
                        <td>
                            <input name="selCompanySerial" id="selCompanySerial" class="easyui-validatebox textbox" style="width:110px;height:22px;" />
                        </td>
                        <th>
                            公司规格型号：
                        </th>
                        <td>
                            <input name="selCModelSize" id="selCModelSize" class="easyui-validatebox textbox" style="width:120px;height:22px;" />
                        </td>
                        <th>
                            设备状态：
                        </th>
                        <td>
                            <select id="selDevState">
                                <option value="1" selected="selected">已出库</option>
                                <option value="2">报废</option>
                            </select>
                        </td>

                        <th>
                            项目质保期：
                        </th>
                        <td>
                            开始时间：
                            <input name="selStartWarrantyTime" id="selStartWarrantyTime" class="easyui-datebox"
                                   editable="false" style="width:110px;height:22px;" />
                        </td>
                        <td>
                            结束时间：
                            <input name="selEndWarrantyTime" id="selEndWarrantyTime" class="easyui-datebox"
                                   editable="false" style="width:110px;height:22px;" />
                        </td>
                        <td>
                            <a href="javascript:void(0);" class="easyui-linkbutton" iconcls="icon-search" plain="true"
                               onclick="ui_Dev_searchdata();">搜索</a>
                            <a href="javascript:void(0);" class="easyui-linkbutton" iconcls="icon-clear" plain="true"
                               onclick="ui_Dev_cleardata();">清空条件</a>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
        <div data-options="region:'center',border:false">
            <table id="ui_DevProRelation_dg" data-options="fit:true,border:false"></table>
        </div>
    </div>
</body>
</html>
